package cn.tedu.ivos.dict.service.impl;


import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.dto.DictSaveParam;
import cn.tedu.ivos.dict.pojo.entity.Dict;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dict.service.DictService;
import cn.tedu.ivos.dictoption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class DictServiceImpl implements DictService {
    @Autowired
    DictMapper dictMapper;
    @Autowired
    DictOptionMapper dictOptionMapper;

    @Override
    public List<DictVO> selectDict(DictQuery dictQuery) {
        List<DictVO> list = dictMapper.selectDict(dictQuery);
        return list;
    }

    @Override
    public void saveDict(DictSaveParam dictSaveParam) {
        log.debug("保存字典业务:dictSaveParam={}", dictSaveParam);
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam, dict);
        if(dict.getId()==null){//新增
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        }else{//更新
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }
    }

    @Override
    public void deleteDict(Long id) {
        log.debug("删除字典业务:id={}", id);
        dictMapper.deleteById(id);
        //删除字典，字典下的所有字典项也要随之删除
        dictOptionMapper.deleteByDictId(id);
    }
}
